//
//  SequenceType+Sum.swift
//  给序列类型扩展求和计算
//
//  Created by yao_yu on 15/7/13.
//  Copyright © 2015年 yao_yu. All rights reserved.
//

import Foundation
import CoreGraphics

protocol Summable {
    
    /// Identity element for this type and the + operation.
    static var zero: Self { get }
    
    /// + operator must be defined for this type
    func +(lhs: Self, rhs: Self) -> Self
}

extension SequenceType where Generator.Element: Summable {
    func sum() -> Generator.Element {
        return self.reduce(Generator.Element.zero, combine: {$0 + $1})
    }
}

extension Int: Summable {
    static var zero: Int { return 0 }
}

extension Float: Summable {
    static var zero: Float { return 0 }
}

extension Double: Summable {
    static var zero: Double { return 0 }
}

extension CGFloat: Summable {
    static var zero: CGFloat { return 0 }
}

extension String: Summable {
    static var zero: String { return "" }
}
